# -*- coding: utf-8 -*-
import smtplib  
import email.MIMEMultipart# import MIMEMultipart  
import email.MIMEText# import MIMEText  
import email.MIMEBase# import MIMEBase  
import os.path  
import mimetypes  
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

From = 'wangpengtongxue@163.com' 
To_all_qq1 = "youjp@tcl.com"
To_all_qq2 = "393769684@qq.com"
To_all_tcl = "w.p@tcl.com"
Tototal_mail=[To_all_qq1,To_all_tcl,To_all_qq2]

file_name = r"/Users/DQA/Desktop/YDMusicBox.ipa"

print '邮箱开始发送'

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))

server = smtplib.SMTP()
server.connect(r'smtp.163.com', 25)
server.ehlo()
#server.starttls()   
server.login("wangpengtongxue@163.com","wangpeng123")

main_msg = email.MIMEMultipart.MIMEMultipart()  

text_msg = email.MIMEText.MIMEText("各位，你好 :     \n  \
        请查收某某项目某某测试报告\n \
默认邮件，请勿回复。如有问题，烦请咨询自动化测试工程师",_charset="utf-8")  
main_msg.attach(text_msg)  

data = open(file_name, 'rb')  
ctype,encoding = mimetypes.guess_type(file_name)  
if ctype is None or encoding is not None:  
    ctype = 'application/octet-stream'  
maintype,subtype = ctype.split('/',1)  
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)  
file_msg.set_payload(data.read())  
data.close( )  
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(file_name)  
file_msg.add_header('Content-Disposition','attachment', filename = basename)
main_msg.attach(file_msg)  
 
main_msg['From'] = _format_addr(u'DQA自动化测试组 <%s>' % From)

#main_msg['Tototal_mail'] = Tototal_mail 
main_msg['Subject'] = "DQA软件自动化测试报告 "  
main_msg['Date'] = email.Utils.formatdate( )  

fullText = main_msg.as_string( )  

try:  
    for val in Tototal_mail:
        main_msg['val'] = val
        server.sendmail(From, val, fullText)  
finally:  
    server.quit()  
print '邮箱开始结束'